package com.ggh.jinjilive.ui.mall.shop;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.mall.MallGoods;
import com.ggh.jinjilive.mobile.mall.shop.AttentionShopMobile;
import com.ggh.jinjilive.mobile.mall.shop.ShopMessageMobile;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class ShopViewModel extends BaseViewModel {
    //获取店铺信息
    public MutableLiveData<ShopMessageMobile> mShopMessageMobile = new MutableLiveData<>();
    //店铺商品
    public MutableLiveData<List<MallGoods>> mMallGoodsList = new MutableLiveData<>();
    //分类ID
    public ObservableField<Integer> type1 = new ObservableField<>(0);
    //分类ID
    public ObservableField<Integer> type2 = new ObservableField<>(0);

    //是否关注
    public MutableLiveData<Boolean> attention = new MutableLiveData<>();
    //店铺列表
    public MutableLiveData<List<AttentionShopMobile>> mAttentionShopMobileList = new MutableLiveData<>();


    public ShopViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }

    //获取店铺信息
    public void storeInfo(int ShopId){
        Map<String, Object> map = new HashMap<>();
        map.put("id",ShopId);
        addSubscribe(apiCallInterface.storeInfo(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<ShopMessageMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                ShopMessageMobile shopMessageMobile = (ShopMessageMobile) resultEntity.getData();
                mShopMessageMobile.setValue(shopMessageMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }


    /**
     * 店铺商品
     * @param sid
     */
    public void productList(int sid){
        if(isRefresh){
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sid", sid +"");
        map.put("page", page);
        map.put("type1",type1.get() +"");
        map.put("type2",type2.get() +"");
        addSubscribe(apiCallInterface.productList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<MallGoods>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<MallGoods> mallGoodsList = (List<MallGoods>) resultEntity.getData();
                mMallGoodsList.setValue(mallGoodsList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                mMallGoodsList.setValue(null);
            }
        }));
    }


    /**
     * 关注店铺列表
     */
    public void collectStoreList(){
        if(isRefresh){
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        addSubscribe(apiCallInterface.collectStoreList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<AttentionShopMobile>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<AttentionShopMobile> attentionShopMobileList = (List<AttentionShopMobile>) resultEntity.getData();
                mAttentionShopMobileList.setValue(attentionShopMobileList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                mMallGoodsList.setValue(null);
            }
        }));
    }

    /**
     * 添加收藏
     * @param sid
     */
    public void addCollectStore(int sid){
        Map<String, Object> map = new HashMap<>();
        map.put("sid", sid);
        addSubscribe(apiCallInterface.addCollectStore(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                attention.setValue(true);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                mMallGoodsList.setValue(null);
            }
        }));
    }

    /**
     * 取消关注店铺
     * @param ids
     */
    public void delCollectStore(String ids){
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        addSubscribe(apiCallInterface.delCollectStore(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                attention.setValue(false);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }

    /**
     * 商品封面
     */
    public void uploadimages(String path, AddOnClickListener1 addOnClickListener1){
        File file = new File(path);
        RequestBody fileRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), fileRequestBody);
        addSubscribe(apiCallInterface.uploadimages(body).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<CommonMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                CommonMobile commonMobile = (CommonMobile) resultEntity.getData();
                addOnClickListener1.onSkipClicked(commonMobile.getUrl());
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                ToastUtils.make().show(netMsg);
            }

        }));
    }

    /**
     * 修改店铺信息提交
     */
    public void shopStoreStoreEdit(Map<String, Object> map){
        addSubscribe(apiCallInterface.shopStoreStoreEdit(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                ToastUtils.make().show(resultEntity.getMsg());
                //结束修改店铺
                ActivityUtils.finishActivity(ShopModificationActivity.class);
                //关闭我的店铺
                ActivityUtils.finishActivity(MineShopActivity.class);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }

}
